package com.liu.service;


import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.liu.serviceBase.exception.MyException;
import com.liu.utils.ContantProperties;
import com.liu.video.InitClient;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * **************
 * 项目名称: testgulili <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦 <br/>
 * 创建时间: 2021/12/28 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
@Service
public class VideoServiceimpl implements VideoService {

    private static  String  Accesskeyid= "LTAI5tPkw28868MKQPHFN9ED";
    private static  String  Accesskeysecret="kAXof3wgoeDtO6ze73FqGvYSxLoWUh";

    @Override
    public String uploadVideo(MultipartFile file) throws IOException {
        String VideoId = null;
        String accessKeyId = ContantProperties.ACCESS_KEY_ID;
        String accessKeySecret = ContantProperties.ACCESS_KEY_SECRET;
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            //          fileName: 文件的原名称；
//        test.mp4
            String fileName = file.getOriginalFilename();
            //        title: 文件上传的名称
            String title = fileName.substring(0,fileName.lastIndexOf("."));
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            System.out.println("accessKeyId:"+accessKeyId);
            System.out.println("accessKeySecret:" +accessKeySecret);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
            if (response.isSuccess()) {
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                VideoId = response.getVideoId();
            } else {
                //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，
                // 此时需要根据返回错误码分析具体错误原因
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
                System.out.println("response  --++-- 失败");
            }
            return  VideoId;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }finally {
                inputStream.close();
            }


    }

        @Override
        public void deleteVideo(String videoSourceId) {
            String accessKeyId = ContantProperties.ACCESS_KEY_ID;
            String accessKeySecret = ContantProperties.ACCESS_KEY_SECRET;
            DefaultAcsClient client = InitClient.initVodClient(accessKeyId, accessKeySecret);
        DeleteVideoResponse response = new DeleteVideoResponse();
        try {
            DeleteVideoRequest request = new DeleteVideoRequest();
            //支持传入多个视频ID，多个用逗号分隔
//            写入Videoid
            request.setVideoIds(videoSourceId);
            response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");

        } catch (Exception e) {

            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
            throw new MyException(20001, "视频删除失败");

        }

    }

    @Override
    public void DeleteseveralVideo(List videoList) {
        if (ObjectUtils.isEmpty(videoList)||videoList.size()==0){
            new MyException(20001,"视频ID列表为空");
        }
        String accessKeyId = ContantProperties.ACCESS_KEY_ID;
        String accessKeySecret = ContantProperties.ACCESS_KEY_SECRET;
        DefaultAcsClient client = InitClient.initVodClient(accessKeyId, accessKeySecret);
        DeleteVideoResponse response = new DeleteVideoResponse();
        try {
            DeleteVideoRequest request = new DeleteVideoRequest();
            //支持传入多个视频ID，多个用逗号分隔
//            写入Videoid
            System.out.println("========删除多个视频========");

            String join = String.join(",", videoList);
            request.setVideoIds(join);
            response = client.getAcsResponse(request);
            System.out.println(join);
            System.out.print("RequestId = " + response.getRequestId() + "\n");

        } catch (Exception e) {

            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
            throw new MyException(20001, "视频删除失败,视频ID列表为空");

        }
    }

    @Override
    public String getVideoPlayAuth(String videoSourceId) throws ClientException {

        // 初始化对象
        DefaultAcsClient client = InitClient.initVodClient(Accesskeyid, Accesskeysecret);
//        创建获取视频凭证的 request和response
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest ();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse ();
//        设置视频id
        request.setVideoId(videoSourceId);
        response = client.getAcsResponse(request);
        String playAuth = response.getPlayAuth();
        //播放凭证
        System.out.print("PlayAuth = " + playAuth + "\n");
        return playAuth;
    }
}
